import React ,{Component} from 'react';
import {
    View,
    Text,
} from 'react-native';
//自定义组件
import Login from '../userCenter/login';
import ConfirmOrder from '../order/confirmOrder';
import MyStroage from '../common/myStroage';
import Header from '../common/header';
import * as URLConstant from '../constant/URLconstant'
import Util from '../common/util'
import Toast from '../common/ToastUtil'
export default class logingoods extends Component {
    // 构造
    constructor(props) {
        super(props);
        // 初始状态

        this.onLoginSuccess = this.onLoginSuccess.bind(this);
        this.onRequestData = this.onRequestData.bind(this);
        this.onLoginSuccess1 = this.onLoginSuccess1.bind(this);
        this.state = {
            datas: [],
            isLogin: false,
            loaded: true,
            titleType: 1,
            spec_id: this.props.spec_id,
            buyNum: this.props.buyNum,
            goods_id: this.props.goods_id

        };
    }

    async componentDidMount() {
        try {
            let that = this;
            let userInfoDate = await MyStroage.getStorageAllDataForKey('userinfo')
            //LOG('KEY11111======' + JSON.stringify(userInfoDate));
            if (userInfoDate == null) {
                that.setState({isLogin: false, loaded: false});
                return;
            } else {
                let user_id = userInfoDate.data.user_id;
                that.onRequestData(user_id);

            }

        } catch (err) {
            LOG(err);
        }

    }

    async componentWillReceiveProps(nextProps) {
        try {
            let that = this;
            let userInfoDate = await MyStroage.getStorageAllDataForKey('userinfo')
            //LOG('KEY11111======' + JSON.stringify(userInfoDate));
            if (userInfoDate == null || userInfoDate == '') {
                that.setState({isLogin: false, loaded: false});
            } else {
                let user_id
                user_id = userInfoDate.data.user_id;
                that.onRequestData(user_id);

            }
        } catch (err) {
            LOG(err);
        }
    }

    //登录成功
    onLoginSuccess(user_id) {
        this.onRequestData(user_id)
        this.setState({
            loaded: true,
        })
    }

    //购物车操作调用
    onLoginSuccess1(user_id) {
        this.onRequestData(user_id);
    }

    onRequestData(user_id) {
                this.setState({
                    user_id: user_id,
                    isLogin: true,
                    loaded: false,
                });

    }




    render() {
        let loaded = this.state.loaded;
        let isLogin = this.state.isLogin;
        let spec_id = this.state.spec_id;
        let buyNum = this.state.buyNum;
        let goods_id = this.state.goods_id
        let user_id = this.state.user_id
        return (
            <View style={{flex:1}}>
                {loaded ?
                    <View style={{flex:1}}>
                        {Util.loading()}
                    </View>
                    :
                    <View style={{flex:1}}>
                        {isLogin ?
                            <View style={{flex:1}}>
                                <ConfirmOrder navigator={this.props.navigator} data={{spec_id,buyNum,goods_id,user_id}}
                                              onLoginSuccess1={this.onLoginSuccess1} type={2}/>
                            </View>
                            :
                            <Login navigator={this.props.navigator}
                                   onLoginSuccess={this.onLoginSuccess}/>
                        }
                    </View>
                }
            </View>
        )
    }
}
